package top.pcat.service;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import top.pcat.entity.ANSI;
import top.pcat.entity.Message;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ClientHandler extends SimpleChannelInboundHandler<Message> {

    private String senderId;
    private String senderName;

    private String roomName = "服务器";

    private long roomId = 0;

    private  Map<Long,String> roomMap = null;

    public ClientHandler(){}

    public ClientHandler(String senderId,String senderName,Map<Long,String> roomMap,String roomName,long roomId){
        this.senderId = senderId;
        this.senderName = senderName;
        this.roomMap = roomMap;
        this.roomId = roomId;
    }


    private SimpleDateFormat sf = new SimpleDateFormat("[HH:mm:ss]");

    /**
     * 处理收到的消息
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message msg)
            throws Exception {

        log.info("来自服务器的消息 ----->  "+msg);
        Channel channel = ctx.channel();
        //log.info("test roomName"+roomName);
        if (msg.getController() == 1){
            switch(msg.getMesType()) {
                case "chooseChatRoom":

                    log.info(ANSI.ANSI_CYAN+"来自" + Client.roomName + "的消息 ----->  "+ANSI.ANSI_RESET+ msg.getMesContent());
                    System.out.println(ANSI.ANSI_GREEN+"║ "+ANSI.ANSI_BLUE+sf.format(msg.getDate())+ANSI.ANSI_PURPLE+ANSI.ANSI_BOLD+" [Not added]"+ANSI.ANSI_GREEN+" System: "+msg.getMesContent());

                    Client.roomMap = parseString(msg.getMesContent());
                    //log.info("ch:"+Client.roomMap);
                    roomName = Client.roomMap.get(Client.roomId);
                    log.info(Client.roomId+"chszrn"+Client.roomName);
                    break;

                    //[Fri May 26 12:20:12 CST 2023] [room2] pcat: hi,what`s your name

                default:
                    System.out.println(ANSI.ANSI_GREEN+"║ "+ANSI.ANSI_BLUE+sf.format(msg.getDate())+ANSI.ANSI_PURPLE+ANSI.ANSI_BOLD+" ["+Client.roomName+"]"+ANSI.ANSI_GREEN+ANSI.ANSI_BOLD+" "+"System"+": "+msg.getMesContent()+ANSI.ANSI_RESET);

                    log.info(ANSI.ANSI_CYAN+"来自" + Client.roomName + "的消息 ----->  "+ANSI.ANSI_RESET+ msg.getMesContent());
                    break;
            }
        }else{
            System.out.println(ANSI.ANSI_GREEN+"║ "+ANSI.ANSI_BLUE+sf.format(msg.getDate())+ANSI.ANSI_PURPLE+ANSI.ANSI_BOLD+" ["+Client.roomName+"]"+ANSI.ANSI_GREEN+ANSI.ANSI_BOLD+" "+msg.getSenderName()+": "+ANSI.ANSI_RESET+ANSI.ANSI_RESET+msg.getMesContent());

            log.info(ANSI.ANSI_CYAN+"来自"+Client.roomName+"的消息 ----->  "+msg.getMesContent());
        }

    }
    /**
     * 连接异常后触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        ctx.close();

    }

    public static Map<Long, String> parseString(String str) {
        Map<Long, String> resultMap = new HashMap<>();
        Pattern pattern = Pattern.compile("(\\d+)\\.(\\w+)");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            long key = Long.parseLong(matcher.group(1));
            String value = matcher.group(2);
            //log.info(key+"正则"+value);
            resultMap.put(key, value);
        }

        //log.info("ps:"+resultMap);
        return resultMap;
    }
}
